import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

//解决编程式路由往同一地址跳转时会报错的情况
const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;
//push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject)
        return originalPush.call(this, location, onResolve, onReject);
    return originalPush.call(this, location).catch(err => err);
};
//replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
    if (onResolve || onReject)
        return originalReplace.call(this, location, onResolve, onReject);
    return originalReplace.call(this, location).catch(err => err);
};

const routes = [{
        path: '',
        redirect: '/home'
    },
    {
        path: '/home',
        name: 'home',
        component: () =>
            import ('@/view/home/Home.vue'),
    },
    {

        path: '/found',
        component: () =>
            import ('@/view/found/Found.vue')
    },
    {
        path: '/libaray',
        component: () =>
            import ('@/view/libaray/Libaray.vue')
    },
    //歌单详情
    {
        path: '/songListDeatil/:id',
        name: 'songListDeatil',
        component: () =>
            import ('@/components/songListDetail/SongListDetail.vue')
    },
    //专辑详情
    {
        path: '/abumetail/:id',
        name: 'abumetail',
        component: () =>
            import ('@/components/albumDetail/AlbumDetail.vue')
    },
    //登录页
    {
        path: '/login',
        component: () =>
            import ('@/view/login/Login.vue')
    },
    //mv详情
    {
        path: '/mvDetail/:id',
        component: () =>
            import ('@/components/video/MvDetail.vue')
    },
    //视频详情页
    {
        path: '/videoDetail/:id',
        component: () =>
            import ('@/components/video/VideoDetail.vue')
    },
    //每日推荐
    {
        path: '/dayMusic',
        component: () =>
            import ('@/components/dayMusic/DayMusic.vue')
    },
    //私人fm
    {
        path: '/fm',
        component: () =>
            import ('@/components/dayMusic/FM.vue')
    },
    //获取歌手详细信息
    {
        path: '/singerInfo/:id',
        component: () =>
            import ('@/components/singer/SingerInfo.vue')
    },
    //获取用户喜欢音乐
    {
        path: '/userLove/:id',
        component: () =>
            import ('@/components//userLove/UserLove.vue')
    },
    //获取最新专辑
    {
        path: '/newAlbum',
        component: () =>
            import ('@/components/newAlbum/NewAlbum.vue')
    },
    //获取搜素首页
    {
        path: '/searchHome/:keyword',
        component: () =>
            import ('@/components/search/SearchHome.vue')
    },
    //搜索歌手详情
    {
        path: '/searchSingerDetail/:keyword',
        component: () =>
            import ('@/components/searchList/SearchSingerDetail.vue')
    },
    //搜索专辑详情
    {
        path: '/searchAlbumDetail/:keyword',
        component: () =>
            import ('@/components/searchList/SearchAlbumDetail.vue')
    },
    //搜索视频详情
    {
        path: '/searchVideoDetail/:keyword',
        component: () =>
            import ('@/components/searchList/SearchVideoDetail.vue')
    },
    //搜索歌曲详情
    {
        path: '/searchSongDetail/:keyword',
        component: () =>
            import ('@/components/searchList/SearchSongDetail.vue')
    },
    //搜索歌单详情
    {
        path: '/searchListDetail/:keyword',
        component: () =>
            import ('@/components/searchList/SearchListDetail.vue')
    },
    //即将播放列表
    {
        path: '/playList',
        component: () =>
            import ('@/components/playList/PlayList.vue')
    },


]


const router = new VueRouter({
    routes,
    mode: "history"
})
router.beforeEach((to, from, next) => {
    // console.log(to);
    if (to.path == '/libaray' || to.path == '/dayMusic' || to.path == '/fm') {
        const loginStatus = sessionStorage.getItem('loginStatus') ? sessionStorage.getItem('loginStatus') : ""
        if (loginStatus !== 'true') {
            next('/login')
        } else {
            next()
        }
    } else {
        next()
    }
});

export default router